﻿/***************************************************/
/*** file:Session.cs                             ***/
/*** date:2010-08-15                             ***/
/*** author:huas                                 ***/
/*** function:session manage                     ***/
/***************************************************/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using IM.Common;

namespace IM.Logic
{
    class SessionMgr
    {
        private Hashtable m_sessionTable;
        private Stack<Session> m_sessionPool;

        private static SessionMgr m_Instance = null;
        private SessionMgr()
        {
            //
            m_sessionTable = new Hashtable(10000);
            m_sessionPool = new Stack<Session>(10000);
            for (int i = 0; i < 10000; i++)
            {
                Session s = new Session();
                m_sessionPool.Push(s);
            }
        }

        public static SessionMgr Instance()
        {
            if (m_Instance == null)
            {
                m_Instance = new SessionMgr();
            }
            return m_Instance;
        }

        /// <summary>
        /// nret = 0 suc; nret = -1 param error
        /// nret = -2 not find
        /// </summary>
        /// <param name="szAccount"></param>
        /// <param name="session"></param>
        /// <returns></returns>
        public int FindSession(string szAccount, ref Session session)
        {
            int nRet = -2;
            session = null;
            if (szAccount == null || szAccount.Length <= 0)
            {
                nRet = -1;
            }
            lock (m_sessionTable)
            {
                foreach (Session s in m_sessionTable.Values)
                {
                    if (s.Account == szAccount)
                    {
                        session = s;
                        nRet = 0;
                        break;
                    }
                }
            }
            return nRet;
        }
        //return -2 param error
        //return 0 return suc
        public int FindSession(int nAccountId, ref Session session)
        {
            int nRet = 0;
            if (nAccountId <= 0)
            {
                return -2;
            }
            lock (m_sessionTable)
            {
                if (m_sessionTable.Contains(nAccountId))
                {
                    session = (Session)m_sessionTable[nAccountId];
                    nRet = 0;
                }
            }
            return nRet;
        }

        //return 0  suc
        //return -1 exists
        //return -2 param error
        //return -3 createsession failed
        public int CreateSession(int nAccountId, ref Session session)
        {
            if (nAccountId <= 0)
            {
                return -2;
            }
            int nRet = 0;
            lock (m_sessionTable)
            {
                if (m_sessionTable.Contains(nAccountId))
                {
                    session = (Session)m_sessionTable[nAccountId];
                    return -1;
                }
                session = m_sessionPool.Pop();
                if (session == null)
                {
                    return -3;
                }
                //create suc, 
                session.SessionMgr = this;
                m_sessionTable[nAccountId] = session;
            }
            return nRet;
        }

        public void DeleteSession(Session session)
        {
            if (session == null || session.Account == null)
            {
                return;
            }
        
            lock (m_sessionTable)
            {
                //删除会话
                if (m_sessionTable.Contains(session.AccountId))
                {
                    m_sessionTable.Remove(session.AccountId);
                }
                //回收
                m_sessionPool.Push(session);
            }
        }
    }
}
